Quick Index
Videos


A quick video on this chapter is here....

START HERE


Before starting take a quite detour through abstraction.

Then, you may want to browse down this page or you could jump to "Examples" and then refer back as needed.

Overview


Idea mode is formulating ideas to solve a problem.

The purpose is to take big complex solutions and let them evaporate down to small simple solutions.

We should force ourself into this mode before coding and then during the coding effort as well.

Without and With Idea Mode


Assume our challenge is to code up three graphic elements.

The listing here is our TO-DO list for the project -- we need to implement all these methods for these three classes.

With "code mode" only we start working on this large set of methods.
Circle        Rectangle      Arc
Methods       Methods        Methods
-------------------------------------
moveBy        moveBy         moveBy
moveTo        moveTo         moveTo
middle        middle         middle
getTop        getTop         getTop
getLeft       getLeft        getLeft
getRight      getRight       getRight
getBottom     getBottom      getBottom
contains      contains       contains

Note: Trival methods not shown
(getters, delegators, ...)
This time we first go into idea-mode before coding.

No non-trivial methods to code.

Is this magic? Nothing to code (excluding trivial)?

Don't underestimate idea-mode.

========== Method Listings ==========

Graphic Element Method Listings:

Circle     Rectangle  Arc      Shape
Methods    Methods    Methods  Methods
-------------------------------------
None       None       None     None

Note: Trival methods not shown
(getters, delegators, ...)

Duplication Alert


Overview

The main "alert" we look out for is "code duplication".

When we see code duplication, it is a signal that we should be looking for reuse.

Less is more.

Example

Method Duplication (Reinvention)
Let's look at an example.

We want to code a "CircularGameGraphic" (this is our proposed code or TO-DO)".

One "tool" we find in our toolbox is a class "Circle" as shown.

This is a duplication alert! .

Or we might also call it a re-invention alert.

Coding the graphic independently would be very redudant. So we'll consider reuse (we'll learn how).
Method Duplication in Similar Classes
Another example where we will be coding Circle, Rectangle and Arc.

One look at the methods listings tell us that we have a you know what.

Duplication alert!
Ivar Duplication in in Similar Classes
Similar to the previous alert except this one regarding ivars.

We have the idea that Circle, Rectangle and Arc are similar classes (similar class family).

Duplication alert!

Toolboxes


To find reuse, we use two primary toolboxes:


Our more important reuse strategy is the "Component Toolbox". We always try it first. Secondly we look at inheritance. And if we have a case where we are not sure, try component before inheritance.

Delegated Method


Overview

A delegated method means we are letting another object to the hard work for us. Like this:


The "other object" is often a component/ivar.

Delegation is design gold.

Example

We are given an object of type "Drawing" as shown. The sequence of actions is:

  • The "Drawing" object receives message "getDrawingCenter()"
  • It delegates (redirects) the message to it's component object "rec".


Plug & Play

The obvious advantage is less code.

But also the component (we delegate to) will likely already be tested-out (solid). Thus it's plug & play.


Partially Delegated

We will often have partial delegation.

This means we do a little work in the method, but most of the work is delegated to a component.

This is still design gold.

Ideas Doc ⭐


See the ideas doc page here....

Examples ⭐


These are short examples to browse through. You may find one a domain (subject) of more interest.


Scratch Simulation Design Trick


A helpful trick for designing objects is to do an scratch simulation (scratch sim), scratched with pencil onto paper.

To implement an iterator, it takes very little coding. However, they can be tricky to design. Perfect candidates for scratch sim.

Simple Algorithm

Example #1 - Printing Objects
//Iterating over any object type
while iterator.hasNext()
	print(iterator.next());
Example #2 - Summing
//Iterating over numbers
Sum = 0
while iterator.hasNext()
	sum = sum + iterator.next();
Just Two Methods
Our entire communication with iterator is just using "hasNext" and "next".
While iterator.hasNext()
	doAnything(iterator.next());


Simulation

For an algorithm that looks so simple, it can be a head scratcher determining how to start. Who are the players (ivars)? How are parts are moving?

Time for scratch sim.

A Simulation Run
We start off simulating an iterator walking a short list.
Given this input: [10, 20, 30, 40]
The expected output is shown
10
20
30
40
Scratch Simulation
To produce the output, we need to send "get" multiple times.

We scratch them on our sketch.
10	get(0)
20	get(1)
30	get(2)
40	get(3)
Ivar Harvest 1
We see we need to send "get" to a DynamicArray. We'll call it "array".

Our first ivar of the design: "array"
Scratch Simulation (Revisited)
Now we see we have an index that is increasing: 0, 1, 2, 3
10	get(0)
20	get(1)
30	get(2)
40	get(3)
Ivar Harvest 2
Bingo, our next ivar: "nextIndex"


Design Pieces

Key Pieces

NameTypeDescription
DynArrayIteratorClassThe class to code
arrayDynamicArrayivar that we send "get" to
nextIndexintivar that we use for "get"
and that we increment


Key Actions


Simple Test Code

It's super helpful to have even one simple test ready to go before we start coding. Below is an example.

Simple Test
Here we walk the iterator from start to end and make sure "next" answers 10, 20, 30, 40.
list = [10, 20, 30, 40]
iter = new Iterator(list)
index = 0
while (iter.hasNext())
	Test that iter.next() equals list[index]
	index++


Ready to Code

Now we're ready to code. Rather than just sitting down at the keyboard and trying to figure out this interesting object called an "iterator", we now have a plan.


References


Object Diagrams


Type/Class Diagrams